package com.test.nio_demo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;

import com.sun.jmx.snmp.tasks.ThreadService;
 

public class FileNioDemo  extends Thread {
	private int index = 0;
	private int type = 0;
	private String sourcePath, desPath ;
	private   int bufferSize ;
	public FileNioDemo(int index, int type, String sourcePath, String desPath, int bufferSize) {
		super();
		this.index = index;
		this.type = type;
		this.sourcePath = sourcePath;
		this.desPath = desPath;
		this.bufferSize = bufferSize;
	}


	
	public static void main(String[] args) {
		String sourcePath =  "D:\\jdk-7u51-windows-x64.exe";
		String desPath1  =  "d:\\1jdk-7u51-windows-x64.exe";
		String desPath2  =  "d:\\2jdk-7u51-windows-x64.exe";
		int bufferSize = 4096;
		
		//nio copy
		System.out.println("nio copy ------");
		long t1 = System.currentTimeMillis();
		 FileNioCopyFile(sourcePath,  desPath1,  bufferSize);;
		long t2= System.currentTimeMillis();
		System.out.println("copy over nio === , use time:" + (t2-t1));
		
		// stream copy
		System.out.println("stream copy ------");
		long t3 = System.currentTimeMillis();
		 FileStreamCopyFile(sourcePath, desPath2, bufferSize);
		long t4 = System.currentTimeMillis();
		System.out.println("copy over steam === , use time:" + (t4-t3));
	}
	
	@Override
	public void run() {
		 if(type == 1){
			 FileNioCopyFile(  sourcePath ,  desPath ,   bufferSize) ;
		 }else{
			 this.FileStreamCopyFile(sourcePath, desPath, bufferSize)  ;
		 }
	}
	
	/**
	 * @param sourcePath
	 * @param desPath
	 */
	public static void FileNioCopyFile(String sourcePath ,String desPath , int bufferSize){
		try {

			FileInputStream fis = new FileInputStream(sourcePath);
			FileOutputStream fos = new FileOutputStream(new File(desPath));
			FileChannel inChannel = fis.getChannel();
			FileChannel outChannel = fos.getChannel();
			java.nio.ByteBuffer inBuffer = java.nio.ByteBuffer.allocate(bufferSize);

			int read = 0;
			while ((read = inChannel.read(inBuffer)) != -1) {
				inBuffer.flip();
				outChannel.write(inBuffer);
				inBuffer.clear();
			}

			inChannel.close();
			outChannel.close();
			fis.close();
			fos.close();
			 
			System.out.println("copy over steam === , use time:" +  System.currentTimeMillis());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void FileStreamCopyFile(String sourcePath ,String desPath, int bufferSize) {
		try {
			FileInputStream fis = new FileInputStream(sourcePath);
			FileOutputStream fos = new FileOutputStream(new File(desPath));
			byte[] buffer = new byte[bufferSize];
			int read = 0 ;
			while((read = fis.read(buffer)) != -1){
				fos.write(buffer, 0, buffer.length);
			}
			 
			fis.close();
			fos.close();
			System.out.println("copy over steam === , use time:" +  System.currentTimeMillis());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void nioTest(){
		//读入文件
		String name = "d:\\server.log";
		try {
			FileInputStream is = new FileInputStream(name);
//					BufferedReader bufferReader = new BufferedReader(new InputStreamReader(is,"utf-8"));
//					String readLine = bufferReader.readLine();
//					while(!readLine .equals("")){
//						System.out.println(readLine);
//						 readLine = bufferReader.readLine();
//					}
//					System.out.println("java io ok=====");
			 FileChannel channel =  is.getChannel();
			 java.nio.ByteBuffer byteBuffer =  java.nio.ByteBuffer.allocate(10);
			// System.out.println("开始读数据之前：" + byteBuffer.toString());
			 byteBuffer.clear();
			 int read = 0;
//					while((read = channel.read(byteBuffer)) != -1){
//						System.out.println("读数据中1：" + byteBuffer.toString());
//						String str = new String(byteBuffer.array(),"utf-8");
//						System.out.println(str);
//						
//						
//						byteBuffer.clear();
//						System.out.println("读数据中2：" + byteBuffer.toString());
//						System.out.println("====================");
//					}
			 System.out.println("读数据前11：" + byteBuffer.toString());
			 read = channel.read(byteBuffer) ;
			 System.out.println("read = " + read);
			 String str1 = new String(byteBuffer.array(),"utf-8");
			 System.out.println(str1);
			 System.out.println("读数据后11：" + byteBuffer.toString());
			 byteBuffer.clear();
			 System.out.println("读数据后11-clear：" + byteBuffer.toString());
			 
			 System.out.println("读数据前22：" + byteBuffer.toString());
			 read = channel.read(byteBuffer) ;
			 System.out.println("read = " + read);
			 String str2 = new String(byteBuffer.array(),"utf-8");
			 System.out.println(str2);
			 System.out.println("读数据后22：" + byteBuffer.toString());
			 byteBuffer.clear();
			 System.out.println("读数据后22-clear：" + byteBuffer.toString());
			System.out.println("java nio read over ..");
			channel.close();
			is.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
